package com.yonyou.crm.common.util;

import java.text.SimpleDateFormat;
import java.util.Calendar;

public class DateUtil {
	
	/**
	 * 将给定具体时间，按业务规则返回格式化字符串
	 * time<1min	 	刚刚 
	 * 1min≤time<60min	当天	X分钟前 
	 * 24h≤time      	当天	hh:mm
	 * 24h≤time且Date=1d	年+月+日+星期	hh:mm
	 * 
	 * @param now 以该时间为准进行比较,常用System.currentTimeMillis()
	 * @param toFormat 待格式化的时间,long格式 
	 * @return
	 */
	public static Long JUST = 60*1000L; //1分钟以内
	public static Long CUR_HOUR = 60*60*1000L; //1小时以内
	public static Long CUR_DAY = 24*60*60*1000L; //当天内
	public static String getFormatDate(Long now,Long toFormat) {
		if(toFormat > now){
			return "时间有误，请检查";
		}
		Long last = now - toFormat ;
		if(last < JUST) {
			return "刚刚";
		}
		else if(last < CUR_HOUR) {
			//X分钟前
			Long minute = (last/1000)/60;
			return minute + "分钟前";
		}
		else if(last < CUR_DAY) {
			Calendar c = Calendar.getInstance();
			c.setTimeInMillis(toFormat);
			SimpleDateFormat fmt = new SimpleDateFormat("HH:mm"); 
			return fmt.format(c.getTime());
		}
		else {
			SimpleDateFormat fmt = new SimpleDateFormat("yyyy-MM-dd HH:mm"); 
			Calendar c = Calendar.getInstance();
			c.setTimeInMillis(toFormat);
			return fmt.format(c.getTime());
		}
	}
}
